home *** CD-ROM | disk | FTP | other *** search
/ 45 Great Windows Utilities 6 / 45 Great Windows Utilities Volume 6 MOJO-405 (Mojo Software).iso / rwin / rwin.cc < prev    next >
C/C++ Source or Header  |  1994-07-31  |  6KB  |  225 lines

  1. /*
  2.  * RWindows 0.5a Beta release 2    Windows startup screen randomizer
  3.  * Copyright (C) 1994 Michael Lea
  4.  * 
  5.  * This program is free software; you can redistribute it and/or modify it
  6.  * under the terms of the GNU General Public License as published by the
  7.  * Free Software Foundation; either version 2 of the Licence, or any later
  8.  * version.
  9.  */
  10.  
  11. #include <stdio.h>
  12. #include <strings.h>
  13. #include <time.h>
  14. #include <stdlib.h>
  15. #include "strext.h"
  16.  
  17. // #define DEBUG
  18.  
  19. #define ZIPFILE "system\\startup.zip"           // default zip file
  20. #define MAXSIZE 50416                           // maximum RLE size
  21.  
  22. #define Z_NOFILES -12                           // offsets from end of zip-file
  23. #define Z_OFFSET -6
  24.  
  25. #define Z_FLEN 24                               // offsets from beginning of zip-file
  26. #define Z_NLEN 28
  27. #define Z_NAME 18
  28.  
  29. char *convpath(char *s);                        // convert a unix-style path to a dos-style path
  30. char *stripname(char *s);                       // strip the filename from a full pathname
  31.  
  32. unsigned short gotopos(FILE *zip);              // sets the file pointer to the start of file record
  33.                         // returns no. of files in the zip file
  34.  
  35. unsigned long getfile(FILE *zip, int num, char *file);
  36.                         // gets the numth file name from the zip file
  37.  
  38. int unzip(char *zip, char *file, char *path);   // unzip file from zip
  39.  
  40. int cp(char *path, char *file);                 // copy files into WIN.COM
  41.  
  42. int rm(char *path, char *file);                 // remove file
  43.  
  44. main(int argc, char *argv[])
  45. {
  46.     printf("RWindows 0.5a, Copyright (C) 1994 Michael Lea\n");
  47.     printf("RWindows comes with ABSOLUTELY NO WARRANTY; This is free software,\n");
  48.     printf("and you are welcome to redistribute it under certain conditions;\n");
  49.     printf("see the file \"README.TXT\" for details.\n\n");
  50.  
  51.     char progpath[strlen(argv[0]) + 1];
  52.  
  53. // DETERMINE WHAT DIRECTORY THE PROGRAM IS BEING RUN FROM (the Windows path)
  54.  
  55.     strcpy(progpath, argv[0]);
  56.     convpath(progpath);
  57.     stripname(progpath);
  58.  
  59. // CONSTRUCT THE FULL PATH TO THE ZIP FILE (the System directory)
  60.  
  61.     char filepath[255];
  62.     strcpy(filepath, progpath);
  63.     if (argc > 1)
  64.         strcat(filepath, "system\\", argv[1]);
  65.     else
  66.         strcat(filepath, ZIPFILE);
  67.  
  68. // OPEN THE ZIP-FILE FOR READING
  69.  
  70.     FILE *zfp;
  71.     zfp = fopen(filepath, "rb");
  72.     if (zfp == NULL)
  73.         abort();
  74.  
  75. // READ NUMBER OF FILES IN ZIP FILE & PICK VALID NUMBER (0 to nofiles-1)
  76.  
  77.     unsigned short nofiles = gotopos(zfp);
  78.     srand(time(NULL));
  79.     int rnum = rand() % nofiles;
  80.  
  81. #ifdef DEBUG
  82.     printf("current time: %d\n", time(NULL));
  83.     printf("%d files, using file no. %d\n", nofiles, rnum);
  84. #endif
  85.  
  86. // GET THE FILE NAME TO USE, AND CHECK IS SIZE TO SEE IF IT'S VALID
  87.  
  88.     char fname[255];
  89.     unsigned long fsize = getfile(zfp, rnum, fname);
  90.     if (fsize > MAXSIZE)
  91.     {
  92.         printf("ERROR: Selected file was too big.\n");
  93.         exit(1);
  94.     }
  95.  
  96. // UNZIP THE FILE
  97.  
  98. #ifdef DEBUG
  99.     printf("selected file: %s\nfile size: %d", fname, fsize);
  100. #else
  101.     unzip(filepath, fname, progpath);
  102.  
  103. // CREATE THE NEW "WIN.COM", REMOVE THE .RLE FILE, AND RUN WINDOWS
  104.  
  105.     cp(progpath, fname);
  106.     rm(progpath, fname);
  107.     system("win.com");
  108. #endif
  109. }
  110.  
  111. char *convpath(char *s)
  112. {
  113.     int i;
  114.  
  115. // REPLACE ALL "/"s WITH "\"s (for DOS-based directory structure)
  116.  
  117.     for (i = 0; i < strlen(s); i++)
  118.         if (s[i] == '/') s[i] = '\\';
  119.     return s;
  120. }
  121.  
  122. char *stripname(char *s)
  123. {
  124.     int i;
  125.  
  126. // REMOVE EVERYTHING AFTER THE LAST "\"
  127.  
  128.     for (i = strlen(s) - 1; i >= 0; i--)
  129.         if (s[i] == '\\')
  130.         {
  131.             s[i + 1] = '\0';
  132.             return s;
  133.         }
  134.     return s;
  135. }
  136.  
  137. unsigned short gotopos(FILE *zip)
  138. {
  139.     unsigned short files;
  140.     unsigned long offset;
  141.  
  142. // GET NUMBER OF FILES IN ZIP-FILE
  143.     
  144.     fseek(zip, Z_NOFILES, SEEK_END);
  145.     fread(&files, sizeof(unsigned short), 1, zip);
  146.  
  147. // GET OFFSET FROM BEGINNING OF FILE TO FIND START OF FILE INFORMATION AT
  148.  
  149.     fseek(zip, Z_OFFSET, SEEK_END);
  150.     fread(&offset, sizeof(unsigned long), 1, zip);
  151.  
  152. // MOVE FILE POINTER TO BEGINNING OF FILE INFORMATION & RETURN NUMBER OF FILES
  153.  
  154.     fseek(zip, offset, SEEK_SET);
  155.     return files;
  156. }
  157.  
  158. unsigned long getfile(FILE *zip, int num, char *file)
  159. {
  160.     unsigned int namelen;
  161.     unsigned long filesize;
  162.  
  163. // SKIP OVER num FILES
  164.  
  165.     for(; num > 0; num--)
  166.     {
  167.         fseek(zip, Z_NLEN, SEEK_CUR);
  168.         fread(&namelen, sizeof(unsigned int), 1, zip);
  169.         fseek(zip, (Z_NAME - sizeof(unsigned int) + namelen), SEEK_CUR);
  170.     }
  171.  
  172. // GET SIZE OF FILE, AND LENGTH OF FILE-NAME
  173.  
  174.     fseek(zip, Z_FLEN, SEEK_CUR);
  175.     fread(&filesize, sizeof(unsigned long), 1, zip);
  176.     fread(&namelen, sizeof(unsigned int), 1, zip);
  177.  
  178. // GET THE ACTUAL FILE NAME & RETURN FILE SIZE
  179.  
  180.     fseek(zip, (Z_NAME - sizeof(unsigned int)), SEEK_CUR);
  181.     fread(file, sizeof(char), namelen, zip);
  182.     file[namelen] = '\0';
  183.     return filesize;
  184. }
  185.  
  186. int unzip(char *zip, char *file, char *path)
  187. {
  188.     char cmd[255];
  189.  
  190. #ifdef DEBUG
  191.     printf("unzipping %s\n", file);
  192. #endif
  193.  
  194.     strcpy(cmd, "pkunzip -o ");
  195.     strcat(cmd, zip, " ", path, "system ", file);
  196.     return (system(cmd));
  197. }
  198.  
  199. int cp(char *path, char *file)
  200. {
  201.     char cmd[255];
  202.  
  203. #ifdef DEBUG
  204.     printf("copying files into win.com\n");
  205. #endif
  206.  
  207.     strcpy(cmd, "copy /b ");
  208.     strcat(cmd, path, "system\\win.cnf + ", path, "system\\vgalogo.lgo + ", path);
  209.     strcat(cmd, "system\\", file, " ", path, "win.com");
  210.     return (system(cmd));
  211. }
  212.  
  213. int rm(char *path, char *file)
  214. {
  215.     char fpath[strlen(path)+strlen(file)+1];
  216.  
  217. #ifdef DEBUG
  218.     printf("removing %s\n", file);
  219. #endif
  220.  
  221.     strcpy(fpath, path);
  222.     strcat(fpath, "system\\", file);
  223.     return (remove(fpath));
  224. }
  225.